﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CarLoanSystem.Entity;

namespace CarLoanSystem.DAL
{
    public class EmployeeInfoDAL : DBBase
    {
        public static EmployeeItem Login(string username,string password)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                SqlCommand command = new SqlCommand(@"SELECT [EMPLOYEE_ID],[EMPLOYEE_NAME],[EMPLOYEE_LASTNAME],[EMPLOYEE_AGE]
                                ,[EMPLOYEE_TEL],[EMPLOYEE_PERSONAL_ID],[EMPLOYEE_POSITION],[EMPLOYEE_GROUP]
                                ,[USERNAME],[PASSWORD] FROM [CAR_LOAN].[dbo].[EMPLOYEE] WHERE [USERNAME] = @USERNAME And [PASSWORD] = @PASSWORD", DBBase.con);
                command.Parameters.AddWithValue("@USERNAME", username.ToLower());
                command.Parameters.AddWithValue("@PASSWORD", password.ToLower());
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                }
                DBBase.DisConncetDatabase();
                if (dt.Rows.Count > 0)
                {
                    EmployeeItem item = new EmployeeItem();
                    item.EMPLOYEE_ID = Convert.ToInt32(dt.Rows[0]["EMPLOYEE_ID"].ToString());
                    item.EMPLOYEE_NAME = dt.Rows[0]["EMPLOYEE_NAME"].ToString();
                    item.EMPLOYEE_LASTNAME = dt.Rows[0]["EMPLOYEE_LASTNAME"].ToString();
                    item.EMPLOYEE_AGE = Convert.ToInt32(dt.Rows[0]["EMPLOYEE_AGE"].ToString());
                    item.EMPLOYEE_TEL = dt.Rows[0]["EMPLOYEE_TEL"].ToString();
                    item.EMPLOYEE_PERSONAL_ID = dt.Rows[0]["EMPLOYEE_PERSONAL_ID"].ToString();
                    item.EMPLOYEE_POSITION = dt.Rows[0]["EMPLOYEE_POSITION"].ToString();
                    item.EMPLOYEE_GROUP = dt.Rows[0]["EMPLOYEE_GROUP"].ToString();
                    item.USERNAME = dt.Rows[0]["USERNAME"].ToString();
                    item.PASSWORD = dt.Rows[0]["PASSWORD"].ToString();
                    return item;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static bool CheckUser(string username)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                SqlCommand command = new SqlCommand(@"SELECT [EMPLOYEE_ID],[EMPLOYEE_NAME],[EMPLOYEE_LASTNAME],[EMPLOYEE_AGE]
                                ,[EMPLOYEE_TEL],[EMPLOYEE_PERSONAL_ID],[EMPLOYEE_POSITION],[EMPLOYEE_GROUP]
                                ,[USERNAME],[PASSWORD] FROM [CAR_LOAN].[dbo].[EMPLOYEE] WHERE [USERNAME] = @USERNAME", DBBase.con);
                command.Parameters.AddWithValue("@USERNAME", username.ToLower());
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                }
                DBBase.DisConncetDatabase();
                if (dt.Rows.Count > 0)
                {
                   
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static DataTable LoadAllEmployee(string id = "", string name = "", string lastName = "")
        {
            try
            {
                string sqlQuery = @"SELECT [EMPLOYEE_ID],[EMPLOYEE_NAME],[EMPLOYEE_LASTNAME],[EMPLOYEE_AGE]
                                ,[EMPLOYEE_TEL],[EMPLOYEE_PERSONAL_ID],[EMPLOYEE_POSITION],[EMPLOYEE_GROUP]
                                ,[USERNAME] FROM [CAR_LOAN].[dbo].[EMPLOYEE] where 1=1 ";
                
                if (id != "")
                {
                    sqlQuery += "and [EMPLOYEE_ID] = " + id;
                }
                if (name != "")
                {
                    sqlQuery += "and [EMPLOYEE_NAME] like '%" + name +"%'";
                }
                if (lastName != "")
                {
                    sqlQuery += "and [EMPLOYEE_LASTNAME] like '%" + lastName + "%'";
                }

                DBBase.ConncetDatabase();

                SqlCommand command = new SqlCommand(sqlQuery, DBBase.con);
                SqlDataReader dr = command.ExecuteReader();

                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                    DBBase.DisConncetDatabase();
                    return dt;
                }
                else
                {
                    DBBase.DisConncetDatabase();
                    return null;
                }
                
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static DataTable LoadAllHeadEmployee(string group = "")
        {
            try
            {
                string sqlQuery = @"SELECT EMPLOYEE_ID, EMPLOYEE_NAME, EMPLOYEE_LASTNAME, EMPLOYEE_AGE, EMPLOYEE_TEL, 
                                EMPLOYEE_PERSONAL_ID, EMPLOYEE_POSITION, EMPLOYEE_GROUP, USERNAME FROM dbo.EMPLOYEE
                                WHERE (EMPLOYEE_GROUP = '" + group + "') AND (EMPLOYEE_POSITION = 'h')";

                DBBase.ConncetDatabase();
                SqlCommand command = new SqlCommand(sqlQuery, DBBase.con);
                SqlDataReader dr = command.ExecuteReader();

                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                    DBBase.DisConncetDatabase();
                    return dt;
                }
                else
                {
                    DBBase.DisConncetDatabase();
                    return null;
                }

            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static EmployeeItem LoadEmployee(string id)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                SqlCommand command = new SqlCommand(@"SELECT [EMPLOYEE_ID],[EMPLOYEE_NAME],[EMPLOYEE_LASTNAME],[EMPLOYEE_AGE]
                                ,[EMPLOYEE_TEL],[EMPLOYEE_PERSONAL_ID],[EMPLOYEE_POSITION],[EMPLOYEE_GROUP]
                                ,[USERNAME],[PASSWORD]  FROM [CAR_LOAN].[dbo].[EMPLOYEE] WHERE [EMPLOYEE_ID] = @EMPLOYEE_ID", DBBase.con);
                command.Parameters.AddWithValue("@EMPLOYEE_ID", id.ToLower());
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                }
                DBBase.DisConncetDatabase();
                if (dt.Rows.Count > 0)
                {
                    EmployeeItem item = new EmployeeItem();
                    item.EMPLOYEE_ID = Convert.ToInt32(dt.Rows[0]["EMPLOYEE_ID"].ToString());
                    item.EMPLOYEE_NAME = dt.Rows[0]["EMPLOYEE_NAME"].ToString();
                    item.EMPLOYEE_LASTNAME = dt.Rows[0]["EMPLOYEE_LASTNAME"].ToString();
                    item.EMPLOYEE_AGE = Convert.ToInt32(dt.Rows[0]["EMPLOYEE_AGE"].ToString());
                    item.EMPLOYEE_TEL = dt.Rows[0]["EMPLOYEE_TEL"].ToString();
                    item.EMPLOYEE_PERSONAL_ID = dt.Rows[0]["EMPLOYEE_PERSONAL_ID"].ToString();
                    item.EMPLOYEE_POSITION = dt.Rows[0]["EMPLOYEE_POSITION"].ToString();
                    item.EMPLOYEE_GROUP = dt.Rows[0]["EMPLOYEE_GROUP"].ToString();
                    item.USERNAME = dt.Rows[0]["USERNAME"].ToString();
                    item.PASSWORD = dt.Rows[0]["PASSWORD"].ToString();
                    return item;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static bool Insert(EmployeeItem item)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlCommand command = new SqlCommand(@"INSERT INTO [CAR_LOAN].[dbo].[EMPLOYEE]
                                                       ([EMPLOYEE_NAME]
                                                       ,[EMPLOYEE_LASTNAME]
                                                       ,[EMPLOYEE_AGE]
                                                       ,[EMPLOYEE_TEL]
                                                       ,[EMPLOYEE_PERSONAL_ID]
                                                       ,[EMPLOYEE_POSITION]
                                                       ,[EMPLOYEE_GROUP]
                                                       ,[USERNAME]
                                                        ,[PASSWORD])
                                                 VALUES
                                                      ( @EMPLOYEE_NAME
                                                       ,@EMPLOYEE_LASTNAME
                                                       ,@EMPLOYEE_AGE
                                                       ,@EMPLOYEE_TEL
                                                       ,@EMPLOYEE_PERSONAL_ID
                                                       ,@EMPLOYEE_POSITION
                                                       ,@EMPLOYEE_GROUP
                                                       ,@USERNAME
                                                       ,@PASSWORD)", DBBase.con);

                command.Parameters.AddWithValue("@EMPLOYEE_NAME", item.EMPLOYEE_NAME);
                command.Parameters.AddWithValue("@EMPLOYEE_LASTNAME", item.EMPLOYEE_LASTNAME);
                command.Parameters.AddWithValue("@EMPLOYEE_AGE", item.EMPLOYEE_AGE);
                command.Parameters.AddWithValue("@EMPLOYEE_TEL", item.EMPLOYEE_TEL );
                command.Parameters.AddWithValue("@EMPLOYEE_PERSONAL_ID", item.EMPLOYEE_PERSONAL_ID);
                command.Parameters.AddWithValue("@EMPLOYEE_POSITION", item.EMPLOYEE_POSITION);
                command.Parameters.AddWithValue("@EMPLOYEE_GROUP", item.EMPLOYEE_GROUP);
                command.Parameters.AddWithValue("@USERNAME", item.USERNAME.ToLower());
                command.Parameters.AddWithValue("@PASSWORD", item.PASSWORD.ToLower());
                int tmp = command.ExecuteNonQuery();
                DBBase.DisConncetDatabase();
                return true;
            }
            catch (Exception ex)
            {
                DBBase.DisConncetDatabase();
                throw ex;
            }
        }

        public static bool Update(EmployeeItem item)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlCommand command = new SqlCommand(@"UPDATE [CAR_LOAN].[dbo].[EMPLOYEE]
                                               SET [EMPLOYEE_NAME] = @EMPLOYEE_NAME
                                                  ,[EMPLOYEE_LASTNAME] = @EMPLOYEE_LASTNAME
                                                  ,[EMPLOYEE_AGE] = @EMPLOYEE_AGE
                                                  ,[EMPLOYEE_TEL] = @EMPLOYEE_TEL
                                                  ,[EMPLOYEE_PERSONAL_ID] = @EMPLOYEE_PERSONAL_ID
                                                  ,[EMPLOYEE_POSITION] = @EMPLOYEE_POSITION
                                                  ,[EMPLOYEE_GROUP] = @EMPLOYEE_GROUP
                                                  ,[USERNAME] = @USERNAME
                                             WHERE ([EMPLOYEE_ID] = '@EMPLOYEE_ID')", DBBase.con);

                command.Parameters.AddWithValue("@EMPLOYEE_ID", item.EMPLOYEE_ID);
                command.Parameters.AddWithValue("@EMPLOYEE_NAME", item.EMPLOYEE_NAME);
                command.Parameters.AddWithValue("@EMPLOYEE_LASTNAME", item.EMPLOYEE_LASTNAME);
                command.Parameters.AddWithValue("@EMPLOYEE_AGE", item.EMPLOYEE_AGE);
                command.Parameters.AddWithValue("@EMPLOYEE_TEL", item.EMPLOYEE_TEL);
                command.Parameters.AddWithValue("@EMPLOYEE_PERSONAL_ID", item.EMPLOYEE_PERSONAL_ID);
                command.Parameters.AddWithValue("@EMPLOYEE_POSITION", item.EMPLOYEE_POSITION);
                command.Parameters.AddWithValue("@EMPLOYEE_GROUP", item.EMPLOYEE_GROUP);
                command.Parameters.AddWithValue("@USERNAME", item.USERNAME.ToLower());
                int tmp = command.ExecuteNonQuery();
                DBBase.DisConncetDatabase();
                return true;
            }
            catch(Exception ex)
            {
                DBBase.DisConncetDatabase();
                throw ex;
            }
        }

        public static int LoadEmployeeLitigation()
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                SqlCommand command = new SqlCommand(@"SELECT [EMPLOYEE_ID]
                FROM [CAR_LOAN].[dbo].[EMPLOYEE] WHERE [EMPLOYEE_POSITION] = 'l'", DBBase.con);
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                }
                DBBase.DisConncetDatabase();
                if (dt.Rows.Count > 0)
                {
                    return Convert.ToInt32(dt.Rows[0]["EMPLOYEE_ID"].ToString());
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
